/************************************************************
	File:		Emitter.cpp
	Author:		Mitchel Mishiev
	Purpose:	CEmitter class creates the emitter of the Game
************************************************************/

#include "Emitter.h"


CEmitter::CEmitter(void)
{
	MaxParticle = 0;
	width = 0;
	height = 0;
	PositionX = 0;
	PositionY = 0;
	particleList.resize(MaxParticle);
	ImageID = 0;
	MaxLife = 0.0f;
	MinLife = 0.0f;
	StartColorAlpha = 0;
	EndColorAlpha = 0;
	StartColorRed = 0;
	EndColorRed = 0;
	StartColorGreen = 0;
	EndColorGreen = 0;
	StartColorBlue = 0;
	EndColorBlue = 0;
	StartVelocityX = 0.0f;
	StartVelocityY = 0.0f;
	EndVelocityX = 0.0f;
	EndVelocityY = 0.0f;
	StartRotation = 0.0f;
	EndRotation = 0.0f;
	StartScaleX = 0.0f;
	EndScaleX = 0.0f;
	StartScaleY = 0.0f;
	EndScaleY = 0.0f;
}


CEmitter::~CEmitter(void)
{
	for( unsigned int i = 0; i < particleList.size(); i++)
	{
		delete particleList[i];
		particleList[i] = nullptr;
	}
}

CEmitter::CEmitter( const CEmitter& emit )
{

		MaxParticle		= emit.MaxParticle;
		width			= emit.width;
		height			= emit.height;
		PositionX		= emit.PositionX;
		PositionY		= emit.PositionY;
		ImageID         = emit.ImageID;
		MaxLife			= emit.MaxLife;
		MinLife			= emit.MinLife;
		StartColorAlpha = emit.StartColorAlpha;
		EndColorAlpha	= emit.EndColorAlpha;
		StartColorRed   = emit.StartColorRed;
		EndColorRed		= emit.EndColorRed;
		StartColorGreen = emit.StartColorGreen;
		EndColorGreen	= emit.EndColorGreen;
		StartColorBlue  = emit.StartColorBlue;
		EndColorBlue	= emit.EndColorBlue;
		StartVelocityX  = emit.StartVelocityX;
		StartVelocityY  = emit.StartVelocityY;
		EndVelocityX    = emit.EndVelocityX;
		EndVelocityY    = emit.EndVelocityY;
		StartRotation   = emit.StartRotation;
		EndRotation     = emit.EndRotation;
		StartScaleX     = emit.StartScaleX;
		EndScaleX       = emit.EndScaleX;
		StartScaleY     = emit.StartScaleY;
		EndScaleY       = emit.EndScaleY;
}

CEmitter& CEmitter::operator=( const CEmitter& emit)
{
	//Check
	if( this != &emit )
	{
	
		//Assign the regular variable the data
		MaxParticle		= emit.MaxParticle;
		width			= emit.width;
		height			= emit.height;
		PositionX		= emit.PositionX;
		PositionY		= emit.PositionY;
		ImageID         = emit.ImageID;
		MaxLife			= emit.MaxLife;
		MinLife			= emit.MinLife;
		StartColorAlpha = emit.StartColorAlpha;
		EndColorAlpha   = emit.EndColorAlpha;
		StartColorRed   = emit.StartColorRed;
		EndColorRed		= emit.EndColorRed;
		StartColorGreen = emit.StartColorGreen;
		EndColorGreen	= emit.EndColorGreen;
		StartColorBlue  = emit.StartColorBlue;
		EndColorBlue	= emit.EndColorBlue;
		StartVelocityX  = emit.StartVelocityX;
		StartVelocityY  = emit.StartVelocityY;
		EndVelocityX    = emit.EndVelocityX;
		EndVelocityY    = emit.EndVelocityY;
		StartRotation   = emit.StartRotation;
		EndRotation     = emit.EndRotation;
		StartScaleX     = emit.StartScaleX;
		EndScaleX       = emit.EndScaleX;
		StartScaleY     = emit.StartScaleY;
		EndScaleY       = emit.EndScaleY;

	}

	//Return
	return *this;
}

void CEmitter::SetupParticleList( CEmitter* emitter )
{
	//Set up the Particle Array
	for( int i = 0; i < emitter->GetMaxParticle(); i++)
	{
		//Create the Object
		particle = new CParticle();

		//Set Life
		particle->SetCurrentLife( (float)( rand() % (int)emitter->GetMaxLife() ) );

		//Set Position
		particle->SetpX( (float)emitter->GetX() + (float)( rand() % emitter->GetWidth() ) );
		particle->SetpY( (float)emitter->GetY() + (float)( rand() % emitter->GetHeight() ) );

		//Set Velocity
		particle->SetCurrentVelocityX( ( (float)( abs(rand() % ( (int)emitter->GetEndVelocityX() - (int)emitter->GetStartVelocityX() ) ) ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartVelocityX() + 1 ) );
		particle->SetCurrentVelocityY( ( (float)( abs(rand() % ( (int)emitter->GetEndVelocityY() - (int)emitter->GetStartVelocityY() ) ) ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartVelocityY() + 1 ) );

		//Set Rotation
		particle->SetCurrentRotation( ( emitter->GetEndRotation() - emitter->GetStartRotation() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartRotation() );

		//Set Scale
		particle->SetCurrentScaleX( ( emitter->GetEndScaleX() - emitter->GetStartScaleX() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartScaleX() );
		particle->SetCurrentScaleY( ( emitter->GetEndScaleY() - emitter->GetStartScaleY() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartScaleY() );

		//Set Color
		particle->SetColorAlpha( ( emitter->GetEndColorAlpha() - emitter->GetStartColorAlpha() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartColorAlpha() );
		particle->SetColorRed( ( emitter->GetEndColorRed() - emitter->GetStartColorRed() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartColorRed() );
		particle->SetColorGreen( ( emitter->GetEndColorGreen() - emitter->GetStartColorGreen() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartColorGreen() );
		particle->SetColorBlue( ( emitter->GetEndColorBlue() - emitter->GetStartColorBlue() ) * ( particle->GetCurrentLife() / emitter->GetMaxLife() ) + emitter->GetStartColorBlue() );

		//Add the particle to the list
		emitter->SetParticle( particle );

	}

}